package sort.test4_7;

import sort.test3_30.BucketSort1;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class BucketSort {
    public  int[]  bucketSort(int[] nums){
        if (nums.length==0){
            return null;
        }
        int min=Integer.MAX_VALUE;
        int max=Integer.MIN_VALUE;

        for (int i = 0; i < nums.length; i++) {
            if (nums[i]>max){
                max=nums[i];
            }
            if (nums[i]<min){
                min=nums[i];
            }
        }
        int bucketCount=(max-min)/ nums.length+1;
        List<ArrayList<Integer>>  list=new ArrayList<>();
        for (int i = 0; i <=bucketCount; i++) {
            list.add(new ArrayList<>());
        }
        for (int i = 0; i < nums.length; i++) {
            list.get((nums[i]-min)/ nums.length).add(nums[i]);
        }

        for (ArrayList  a:list
             ) {
            Collections.sort(a);
        }

      int n=0;
        for (ArrayList a:list
             ) {

            for (int i = 0; i < a.size(); i++) {
                nums[n++]= (int) a.get(i);
            }

        }
        return nums;
    }

    public static void main(String[] args) {

        BucketSort  bucketSort1 = new BucketSort();
        int[] arr = {3,44,38,5,47,15,36,26,27,2,46,4,19,50,48};
        bucketSort1.bucketSort(arr);
        System.out.println(Arrays.toString(arr));

    }


}
